/**
@file
	KernelImpl.h
@brief

@author
	GengYong
@revision
	2005/11/05 * Created by GengYong.
*/


#ifndef SAF_HEADER_FILE_KERNELIMPL_H
#define SAF_HEADER_FILE_KERNELIMPL_H

#include "../include/KernelExport.h"

namespace SAF
{	
	namespace KERNEL
	{
		namespace EXCEPTIONCODE
		{
			enum
			{
				TERMINATE_REQUEST,
			};
		}
		template<int> class KERNELEXCEPTION{};
	}

	class KernelImpl:public IKernel
	{
	private:
		friend class KernelLoader;
	private:
		KernelImpl(unsigned long maxmessagedevicecount, unsigned long maxserviceproxycount, unsigned long maxstatecount);
		virtual ~KernelImpl();
	public:
		virtual IMessageRouter * PlugMessageRouter(const char * name, IMessageRouter *);
		virtual IServiceModule * PlugServiceModule(const char * name, IServiceModule *);
		virtual IMessageRouter * FindMessageRouter(const char * name) const;
		virtual IServiceModule * FindServiceModule(const char * name) const;
		virtual int GetMessageRouterList(IMessageRouter**&) const;
		virtual int GetServiceModuleList(IServiceModule**&) const;
		virtual int GetServiceModuleDiffList(int firststateid, int secondstateid, IServiceModule**&) const;
		virtual int GetMessageRouterInitStateId(int index) const;
		virtual const StateBits& GetServiceModuleStateBits(int index) const;
		virtual int GetServiceModuleCatalog(int index) const;
		virtual int AppendStateName(const char * statename);
		virtual int GetStateFromName(const char * statename) const;
		virtual void * InstallModulePlugHook(const char * name, MessageRouterPlugHook, void * cbdata) const;
		virtual void * InstallModulePlugHook(const char * name, ServiceModulePlugHook, void * cbdata) const;
		virtual void UninstallModulePlugHook(void *) const;
		virtual void OutputConsole(const char *, ...) const;
	private:
		typedef struct ServiceModuleList
		{
			IServiceModule ** list;
			unsigned long	  count;
		} ServiceModuleList;

		typedef struct StateNameTree
		{
			const char *	name;
			unsigned long	stateId;
			StateNameTree * lleaf;
			StateNameTree * rleaf;
		} StateNameTree;

		typedef struct HookList
		{	
			HookList	*	prev;
			HookList	*	next;
			void		*	cbdata;
			union
			{
				MessageRouterPlugHook	messageRouterHook;
				ServiceModulePlugHook	serviceModuleHook;
			};
		} HookList;

		typedef struct ModuleTree
		{
			const char	* name;		
			HookList	* hooks;
			ModuleTree	* lleaf;
			ModuleTree	* rleaf;
			union
			{
				IMessageRouter * messageRouter;
				IServiceModule * serviceModule;
			};
		} ModuleTree;
	private:
		int						m_maxCountOfMessageRouter;
		int						m_maxCountOfServiceModule;
		int						m_maxCountOfState;
		IMessageRouter **		m_messageRouterList;
		IServiceModule **		m_serviceModuleList;
		ModuleTree *			m_messageRouterRoot;
		ModuleTree *			m_serviceModuleRoot;
		StateNameTree *			m_stateNameRoot;
		ServiceModuleList *		m_serviceModuleDiffList;
		StateBits *				m_serviceModuleStateTable;
		int *					m_serviceModuleCatalogId;
		int *					m_messageRouterInitState;
		int						m_countOfMessageRouter;
		int						m_countOfServiceModule;
		int						m_countOfState;
	};
}


#endif	//#ifndef SAF_HEADER_FILE_KERNELIMPL_H
